package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.query.TravelCommentQuery;
import cn.wolfcode.wolf2w.repository.TravelCommentRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class TravelCommentServiceImpl implements ITravelCommentService{
    @Autowired
    private MongoTemplate template;

    @Autowired
    private TravelCommentRepository travelCommentRepository;



    @Override
    public void save(TravelComment travelComment) {
        travelComment.setId(null);
        travelComment.setCreateTime(new Date());

        // 评论中的评论
        //维护关联评论 （获取游记评论里面对应的回复评论id）
        String refId = travelComment.getRefComment().getId();
        // 如果回复对应评论的id存在
        if (StringUtils.hasLength(refId)){
            // 将查询到的回复评论对象放到评论中
            travelComment.setRefComment(this.get(refId));
            // 类型为评论中的评论
            travelComment.setType(TravelComment.TRAVLE_COMMENT_TYPE_COMMENT);
        }
        travelCommentRepository.save(travelComment);
    }

    @Override
    public void delete(String id) {
        travelCommentRepository.deleteById(id);
    }

    @Override
    public void update(TravelComment travelComment) {

        travelCommentRepository.save(travelComment);
    }

    @Override
    public TravelComment get(String id) {
        // 查询数据里对应的游记
        TravelComment travelComment = travelCommentRepository.findById(id).get();

        return travelComment;
    }

    @Override
    public List<TravelComment> list() {
        return travelCommentRepository.findAll();
    }

    @Override
    public Page<TravelComment> query(TravelCommentQuery qo) {
        Query query = new Query();

        long totalCount = template.count(query, TravelComment.class);

        if (totalCount == 0){
            return Page.empty();
        }

        // 创建分页添加
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                Sort.Direction.ASC, "_id");

        List<TravelComment> travelComments = template.find(query, TravelComment.class);

        return new PageImpl<>(travelComments,pageable,totalCount);

    }

    @Override
    public List<TravelComment> queryByTravelId(String travelId) {

        // 通过游记id查询对应的所有评论
        List<TravelComment> list = travelCommentRepository.findByTravelId(travelId);

        return list;
    }
}
